home *** CD-ROM | disk | FTP | other *** search
/ Mac100% 1998 November / MAC100-1998-11.ISO.7z / MAC100-1998-11.ISO / オンラインソフト定点観測 / ユーティリティ / Mops 3.2.sea / Mops 3.2 / Mops source / More classes / File+ < prev    next >
Text File  |  1995-07-16  |  5KB  |  178 lines

  1. (* The class File+ provides (among many other things) an interface with the
  2.    file system that simplifies data transfer in accordance with the user
  3.    interface guidelines.  Thus we provide methods %OPEN:, %SAVE: and %SAVEAS:.
  4.    We rather arbitrarily use "%" before the name as Open: is needed for the
  5.    superclass File.
  6.  
  7.    Our basic strategy is that %OPEN: opens the file, transfers all the file
  8.    data to an object in memory, then closes the file.  Likewise %SAVE: and
  9.    %SAVEAS: transfer all the data to a file which is then closed, and the
  10.    volume is flushed as well.  This strategy has a number of advantages.
  11.    It minimizes disk swapping if files on different disks are to be accessed.
  12.    It also gives us the best chance that the disk, and especially its
  13.    directory, will be valid in the event of a power failure or program crash.
  14.    Note also that RAM cache data is written to disk by a volume flush.
  15.  
  16.   The disadvantage is that we must have enough memory to hold all of the
  17.   data of one or more files at once.  This should not be a problem on a Mac
  18.   with 512K or greater, for normal file sizes.  Of course this  approach also
  19.   makes the processing of the data quicker and easier, in  general.
  20.  
  21.   The data movement is accomplished by sending a late-bound SEND: or
  22.   BRING: message to the passed-in object.  This allows the object to
  23.   send out and bring in its data in whatever way it likes.  Obviously a
  24.   class intended  for use with FILE+ must support these methods.  For some
  25.   classes, after  BRING: a FIXUP: message must be sent to the object.  This
  26.   allows any  time-consuming reformatting to be deferred until the I/O
  27.   transfer is over,  allows the possibility of it being run as a separate
  28.   task, and also allows an opportunity for BRING: to use an asynchronous
  29.   read.
  30.  
  31.   For %OPEN:, %SAVE: and %SAVEAS:, the file may or may not already be open.
  32.   If the file is already open, it is reused rather than deleted - this way,
  33.   it stays in the same place in its folder.
  34. *)
  35.  
  36. ¥            ======= Finder info class ========
  37.  
  38. ¥ With the advent of System 7 and AppleEvents, this class is no longer
  39. ¥ relevant.  However many applications will have to be able to run under
  40. ¥ earlier systems.  But if you try to call this class when AppleEvents are
  41. ¥ available, you'll now get an error message.
  42.  
  43. :class    FIN    super{  object  }
  44.  
  45. private
  46. :m   FINFO:    ¥ ( -- addr )
  47.     AppleEvents?  ?error 63   fInfo    ;m
  48. public
  49.  
  50. :m  PRINT?:    fInfo: self   w@x   ;m
  51.  
  52. :m  SIZE:        ¥ ( -- #files )
  53.     fInfo: self   2+ w@   ;m
  54.  
  55. :m  FREC:        ¥ ( idx -- addr )
  56.     fInfo: self   4+
  57.     swap ( idx )  0  ?do  8 +  dup c@ + 1+ align  loop   ;m
  58.  
  59. :m  VREF:    ¥ ( idx -- refNum )
  60.     fRec: self  w@   ;m
  61.  
  62. :m  FTYPE:    ¥ ( idx -- fType )
  63.     fRec: self  2+  @   ;m
  64.  
  65. :m  FVER:    ¥ ( idx -- fVer )
  66.     fRec: self  6 +  c@   ;m
  67.  
  68. :m  FILENAME:    ¥ ( idx -- addr len )
  69.     fRec: self  8 +  count   ;m
  70.  
  71. :m  =: ( idx ) { fileObj -- }
  72.     dup  filename: self  name: [ fileObj ]
  73.     vRef: self  setVref: [ fileObj ]  ;m
  74.  
  75. ;class
  76.  
  77. fin    FINDERINFO
  78.  
  79.  
  80. ¥        =============================
  81.  
  82. :class    FILE+     super{  file  }
  83.  
  84. :m  GETCREATOR:        ¥ ( -- crtr )
  85.     ^base  $ 24 +  @   ;m
  86.  
  87. :m  BEGIN:
  88. ¥ Puts the current position and EOF of the file to the start.  The file must already be open.
  89.     0 moveto: self  OK?
  90.     0 ^base 28 + !  ^base  fdos$ A012  OK?   ;m
  91.  
  92. :m  (%OPEN):   { obj -- }
  93.     openReadOnly: self  OK?
  94.     0 moveto: self  OK?        ¥ In case it was already open
  95.     watchCurs
  96.     ^base  bring: [ obj ]  arrowCurs
  97.     close: self  drop
  98.         ¥ Note: we ignore any error here since if we ran out of
  99.         ¥ memory, NOMEM may have closed the file already.
  100.  ;m
  101.  
  102. :m  (%SAVE):  { obj -- }
  103.     watchCurs
  104.     begin: self   ^base  send: [ obj ]   close: self   OK?
  105.     flushVol: self
  106.     arrowCurs   ;m
  107.  
  108. :m  %OPEN:  ( type1 ... typen n ) { obj -- b }
  109.     close: self  drop            ¥ just in case
  110.     stdGet: self   dup  0EXIT    ¥ out if "cancel" hit
  111.     obj (%open): self   ;m
  112.  
  113.  
  114. ¥ OPENFNDR:  opens the Nth file indicated by the Finder info.
  115.  
  116. :m  OPENFNDR:  { idx obj -- }
  117.     idx ^base  =: finderinfo
  118.     obj (%open): self   ;m
  119.  
  120. :m  %SAVE:  { obj -- }
  121.     open: self  drop
  122.     obj (%save): self   ;m
  123.  
  124. :m  CREATE&SAVE:  ( typ crtr ) { obj -- }
  125.     create: self   OK?   set: self  
  126.     obj (%save): self    ;m
  127.  
  128. :m  %SAVEAS:  ( typ crtr addr1 len1 addr2 len2 ) { obj -- b }
  129.     close: self  drop        ¥ just in case
  130.     stdput: self
  131.     nif  ( cancel hit )  2drop  false  exit  then
  132.     obj create&save: self   true    ;m
  133.  
  134. ¥ GETLINE: is designed to work in the same way as in class WordBatch, so that the caller doesn't have to worry about whether his input is coming from a single file or a batch of files. It reads the next line into the given string and returns TRUE, or, if end file has been reached, sets the length of the string to zero and returns FALSE.
  135.  
  136. :m  GETLINE?:  { str -- b }
  137.     10000  str setsize: string+    ¥ Max line length - change if nec
  138.     str all: string+  readLine: self
  139.     dup -39 =
  140.     if  ( end file )
  141.         drop
  142.         bytesRead: self  dup  str setsize: string+
  143.         0<>
  144.     else
  145.         OK?
  146.         bytesRead: self 1-    ¥ Adjust length to exclude any RET
  147.         str setSize: string+
  148.         true
  149.     then
  150.     str reset: string+   ;m
  151.  
  152. :m  PUTLINE:  { str -- }
  153.     str len: string+
  154.     nif        ¥ empty string - we will output a RET
  155.         true
  156.     else    str get: string+  write: self  OK?
  157.         str last: string+  RET <>
  158.     then
  159.     if  RET pad c!  pad 1  write: self  OK?  then    ;m
  160.  
  161. ;class
  162.  
  163. endload
  164.  
  165. ¥ Testing:
  166. file+ f
  167. string+ s
  168.  
  169. : GO
  170.     new: s  100000 setsize: s
  171.     'type TEXT 1  stdget: f
  172.     open: f   ;
  173. : TEST
  174.     all: s  asynch read: f
  175.     busy .   ;
  176. : ZZ
  177.     release: s  close: f  drop  ;
  178.